package cn.test.my.excel.poi;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author summit
 * @since 2021/6/25 15:58
 */
public class DateUtils {

	private DateUtils() {

	}

	private static final ZoneOffset ZONE = ZoneOffset.of("+8");

	/**
	 * yyyyMMdd
	 */
	public static final DateTimeFormatter DATE_SIMPLE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");

	/**
	 * yyyy-MM-dd
	 */
	public static final DateTimeFormatter DATE_SIMPLE1_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

	/**
	 * yyyyMMdd
	 */
	public static final DateTimeFormatter DATE_YYYYMM_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");

	/**
	 * yyyy-MM-dd
	 */
	public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;

	/**
	 * yyyy年MM月dd日
	 */
	public static final DateTimeFormatter DATE_CN_FORMATTER = DateTimeFormatter
			.ofPattern("yyyy年MM月dd日");

	/**
	 * MM月dd日
	 */
	public static final DateTimeFormatter DATE_MD_FORMATTER = DateTimeFormatter.ofPattern("MM月dd日");

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
			.ofPattern("yyyy-MM-dd HH:mm:ss");

	/**
	 * yyyy年MM月dd日 HH时mm分ss秒
	 */
	public static final DateTimeFormatter DATE_TIME_CN_FORMATTER = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");

	/**
	 * HH:mm:ss
	 */
	public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

	/**
	 * LocalDate->Date
	 *
	 * @param localDate localDate
	 * @return Date
	 */
	public static Date transLocalDateToDate(LocalDate localDate) {
		if (localDate == null) {
			return null;
		}
		return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
	}

	/**
	 * Date -> LocalDate
	 *
	 * @param date date
	 * @return LocalDate
	 */
	public static LocalDate transDateToLocalDate(Date date) {
		if (date == null) {
			return null;
		}
		return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
	}

	/**
	 * Date -> LocalDateTime
	 *
	 * @param date date
	 * @return LocalDateTime
	 */
	public static LocalDateTime transDateToLocalDateTime(Date date) {
		return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
	}

	/**
	 * LocalDateTime -> Date
	 *
	 * @param localDateTime localDateTime
	 * @return Date
	 */
	private static Date transLocalDateTimeToDate(LocalDateTime localDateTime) {
		return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
	}
}
